<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
$_SESSION['sa_thucthi']="add";
include 'operations/connection.php';
$query="select sa_general_allowance.id, sa_general_allowance.allowance_car,sa_general_allowance.allowance_hard,sa_general_allowance.allowance_holiday,sa_general_allowance.allowance_lunch,sa_general_allowance.allowance_overtime,sa_general_allowance.is_status,sa_general_allowance.number_max_holiday,sa_general_allowance.number_time_work from sa_general_allowance order by sa_general_allowance.is_status desc";
$m_query=mysql_query($query);


$_REQUEST["abc"]="0";
$_REQUEST['first_create']=date("Y-m-d");
$_REQUEST['user_create']=$_SESSION['fullname'];
$_REQUEST['user_last_update']="";
$_REQUEST['time_last_update']="";
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.price_format.1.7.js"></script>
<script type="text/javascript" src="js/jquery.price_format.1.7.min.js"></script>
<link	href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function(){
	document.getElementById("number_time_work").focus();
	$('#allowance_holiday').priceFormat({
		prefix: '',
		centsLimit: 0,
		centsSeparator: '',
		thousandsSeparator: ','
	});
	$('#allowance_lunch').priceFormat({
		prefix: '',
		centsLimit: 0,
		centsSeparator: '',
		thousandsSeparator: ','
	});
	$('#allowance_car').priceFormat({
		prefix: '',
		centsLimit: 0,
		centsSeparator: '',
		thousandsSeparator: ','
	});
	$('#allowance_hard').priceFormat({
		prefix: '',
		centsLimit: 0,
		centsSeparator: '',
		thousandsSeparator: ','
	});
	$('#allowance_overtime').priceFormat({
		prefix: '',
		centsLimit: 0,
		centsSeparator: '',
		thousandsSeparator: ','
	});
});

function noloveyou(){
	$(document).ready(function(){
		$('#allowance_holiday').priceFormat({
			prefix: '',
			centsLimit: 0,
			centsSeparator: '',
			thousandsSeparator: ','
		});
		$('#allowance_lunch').priceFormat({
			prefix: '',
			centsLimit: 0,
			centsSeparator: '',
			thousandsSeparator: ','
		});
		$('#allowance_car').priceFormat({
			prefix: '',
			centsLimit: 0,
			centsSeparator: '',
			thousandsSeparator: ','
		});
		$('#allowance_hard').priceFormat({
			prefix: '',
			centsLimit: 0,
			centsSeparator: '',
			thousandsSeparator: ','
		});
		$('#allowance_overtime').priceFormat({
			prefix: '',
			centsLimit: 0,
			centsSeparator: '',
			thousandsSeparator: ','
		});
	});
}

function enterTable(e,value){
    var key = window.event ? e.keyCode : e.which;
    if (key == 13 || key == 8 || key == 46  || key == 37 || key == 39 ) {
    	if(key==13){
    		var j = 0;
    		var listID = new Array();
    		listID = listID.concat(["number_time_work","number_max_holiday","allowance_holiday","allowance_lunch","allowance_car","allowance_hard","allowance_overtime","abc"]);	
    		for (j = 0; j < listID.length; j++){	
    			if (listID[j] == value) {	
    				if (j == listID.length - 1) {
    					return false;
    				}
    				document.getElementById(listID[j + 1]).focus();
    				document.getElementById(listID[j + 1]).select();
    				return false; 
    			}
    		}
    		
    		return false;
    	}else
    		return true;	
    }else if ( key < 48 || key > 57 ) {
        return false;
    }
    else return true;			
}

function checkonly(tenid,field,page){
	if(document.getElementById(tenid).checked == true){
		for(var i = 0;i<field.length;i++)
	    {
			field[i].checked = false ;
	    }
		document.getElementById(tenid).checked = true;
	 	var gtid=document.getElementById(tenid).value;
	 	document.getElementById("hanhdong").value="updel";
		document.getElementById("idgen").value=gtid;
		if(document.getElementById("idgen").value!="null"){
			var iddel=document.getElementById("idgen").value;
			if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		 		xmlhttp=new XMLHttpRequest();
		 	}else{// code for IE6, IE5
		 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		 	}
		 	xmlhttp.onreadystatechange=function(){
				 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
				 		document.getElementById("showinput").innerHTML=xmlhttp.responseText;
				 		noloveyou();
				 	}
			 	}
		 	xmlhttp.open("POST","ajax/salary/sa_viewcheck_general_allowance.php?idgen="+gtid,true);
		 	xmlhttp.send();
		}
	}else{
		document.getElementById(tenid).checked=false;
		resetvalue();
	}
	
 }

function add(user){
	var numtime=document.getElementById("number_time_work").value;
	var numholiday=document.getElementById("number_max_holiday").value;
	var aholiday=document.getElementById("allowance_holiday").value;
	var alunch=document.getElementById("allowance_lunch").value;
	var acar=document.getElementById("allowance_car").value;
	var ahard=document.getElementById("allowance_hard").value;
	var aovertime=document.getElementById("allowance_overtime").value;
	var astatus=$('input[name=abc]:checked').val();

	//alert(numtime+"-"+numholiday+"-"+aholiday+"-"+alunch+"-"+acar+"-"+ahard+"-"+aovertime+"-"+astatus);
	
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
 		xmlhttp=new XMLHttpRequest();
 	}else{// code for IE6, IE5
 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 	}
 	xmlhttp.onreadystatechange=function(){
		 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
		 		document.getElementById("showdata").innerHTML=xmlhttp.responseText;
		 		alert("<?php echo $l_themthanhcong;?>");
		 		resetvalue();
		 	}
	 	}
 	xmlhttp.open("POST","ajax/salary/sa_add_general_allowance.php?hanhdong=add&numtime="+numtime+"&numholiday="+numholiday+"&aholiday="+aholiday+"&alunch="+alunch+"&acar="+acar+"&ahard="+ahard+"&aovertime="+aovertime+"&astatus="+astatus+"&ausercreate="+user,true);
 	xmlhttp.send();
}

function checknulldata(){
	var numtime=document.getElementById("number_time_work").value;
	var numholiday=document.getElementById("number_max_holiday").value;
	var aholiday=document.getElementById("allowance_holiday").value;
	var alunch=document.getElementById("allowance_lunch").value;
	var acar=document.getElementById("allowance_car").value;
	var ahard=document.getElementById("allowance_hard").value;
	var aovertime=document.getElementById("allowance_overtime").value;

	if(numtime=="" || numtime<0){
		alert("not null and >0");
		return false;
	}else if(numholiday=="" || numholiday<0){
		alert("not null and >0");
		return false;
	}else if(aholiday=="" || aholiday<0){
		alert("not null and >0");
		return false;
	}else if(alunch=="" || alunch<0){
		alert("not null and >0");
		return false;
	}else if(acar=="" || acar<0){
		alert("not null and >0");
		return false;
	}else if(ahard=="" || ahard<0){
		alert("not null and >0");
		return false;
	}else if(aovertime=="" || aovertime<0){
		alert("not null and >0");
		return false;
	}else{
		return true;
	}
}

function thucthi_add(user){
	if(document.getElementById("hanhdong").value=="addnew"){
		if(checknulldata()==true){
			add(user);
		}
	}
}

function thucthi_update(user){
	if(document.getElementById("hanhdong").value=="updel"){
		if(checknulldata()==true){
			update();
		}
	}
}

function thucthi_delete(){
	if(document.getElementById("hanhdong").value=="updel"){
		if(document.getElementById("idgen").value!="null"){
			var iddel=document.getElementById("idgen").value;
			hoi= confirm("<?php echo $l_areyousure;?>");
			if (hoi==true) {
				if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
			 		xmlhttp=new XMLHttpRequest();
			 	}else{// code for IE6, IE5
			 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			 	}
			 	xmlhttp.onreadystatechange=function(){
					 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
					 		document.getElementById("showdata").innerHTML=xmlhttp.responseText;
					 		alert("<?php echo $l_xoathanhcong;?>");
					 		resetvalue();
					 		
					 	}				 	}
			 	xmlhttp.open("POST","ajax/salary/sa_add_general_allowance.php?hanhdong=del&idgen="+iddel,true);
			 	xmlhttp.send();
			}
		}
	}
}

function update(){
	var id=document.getElementById("idgen").value;
	if(id!="null"){
		hoi= confirm("<?php echo $l_areyousure;?>");
		if (hoi==true) {
			var numtime=document.getElementById("number_time_work").value;
			var numholiday=document.getElementById("number_max_holiday").value;
			var aholiday=document.getElementById("allowance_holiday").value;
			var alunch=document.getElementById("allowance_lunch").value;
			var acar=document.getElementById("allowance_car").value;
			var ahard=document.getElementById("allowance_hard").value;
			var aovertime=document.getElementById("allowance_overtime").value;
			var astatus=$('input[name=abc]:checked').val();

			if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		 		xmlhttp=new XMLHttpRequest();
		 	}else{// code for IE6, IE5
		 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		 	}
		 	xmlhttp.onreadystatechange=function(){
				 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
				 		document.getElementById("showdata").innerHTML=xmlhttp.responseText;
				 		alert("<?php echo $l_capnhatthanhcong; ?>");
				 		resetvalue();
				 		/*for(var i = 0;i<document.nsa_f_general_allowance.list.length;i++)
					    {
						    if(document.nsa_f_general_allowance.list[i].value==id)
						    	document.nsa_f_general_allowance.list[i].checked = true ;
						    else
								document.nsa_f_general_allowance.list[i].checked = false ;
					    }*/
				 		
				 		//noloveyou();
				 	}
			 	}
		 	xmlhttp.open("POST","ajax/salary/sa_add_general_allowance.php?hanhdong=update&idgen="+id+"&numtime="+numtime+"&numholiday="+numholiday+"&aholiday="+aholiday+"&alunch="+alunch+"&acar="+acar+"&ahard="+ahard+"&aovertime="+aovertime+"&astatus="+astatus,true);
		 	xmlhttp.send();
		}
	}
}

function resetvalue(){
	document.getElementById("number_time_work").value="";
	document.getElementById("number_max_holiday").value="";
	document.getElementById("allowance_holiday").value=0;
	document.getElementById("allowance_lunch").value=0;
	document.getElementById("allowance_car").value=0;
	document.getElementById("allowance_hard").value=0;
	document.getElementById("allowance_overtime").value=0;
	document.getElementById("user_create").value=document.getElementById("userCreate").value;
	document.getElementById("user_last_update").value="";
	document.getElementById("time_last_update").value="";
	document.getElementById("first_create").value="<?php echo date("Y-m-d");?>";
	document.getElementById("hanhdong").value="addnew";
	document.getElementById("idgen").value="null";
	document.getElementById("number_time_work").focus();

	for(var i = 0;i<document.nsa_f_general_allowance.list.length;i++)
    {
		document.nsa_f_general_allowance.list[i].checked=false;
    }
	for(var i = 0;i< document.nsa_f_general_allowance.abc.length;i++)
	{
	   if (document.nsa_f_general_allowance.abc[i].value=="0")
	   {
	      document.nsa_f_general_allowance.abc[i].checked=true;
	   }else{
		   document.nsa_f_general_allowance.abc[i].checked=false;
	   }
	}
	
	noloveyou();
}

</script>
<form id="sa_f_general_allowance" name="nsa_f_general_allowance" method="post" action="">
<div id="contains">
	<div class="title" style="padding-top: 5px;font-weight: bold;"><?php echo $l_accountant;?> &rarr; <?php echo $l_ml_tinh_luong;?> &rarr; <?php echo $l_setup_regulation;?> &rarr; <?=$l_general_allowance?></div>
	<div class="content_code">
		<div class="approval-right">
			<div style="width:100%" id="showdata">
				<table class="cssTable" border="1" >
					<thead>
						<tr>
							<th scope="col" style="width:10px"></th>
							<th scope="col" style="height:45px;"><?php echo $l_number_time_work;?></th>
							<th scope="col"><?php echo $l_number_max_holiday;?></th>
							<th scope="col"><?php echo $l_allowance_holiday;?></th>
							<th scope="col"><?php echo $l_allowance_lunch;?></th>
							<th scope="col"><?php echo $l_allowance_car;?></th>
							<th scope="col"><?php echo $l_allowance_hard;?></th>
							<th scope="col"><?php echo $l_allowance_overtime;?></th>
							<th scope="col"><?php echo $l_Activestatus;?></th>
						</tr>
					</thead>
					<tbody>
						<?php 
							if(mysql_num_rows($m_query)>0){
								$i=0;
								while($row=mysql_fetch_array($m_query)){
								
									if($row[is_status]==1){
										?>
									<tr style="color:green;">
										<?php 
									}else{
									?>
									<tr>
									<?php }?>
									<td style="width: 10px"><div style="text-align:center"><input onclick="checkonly(this.id,document.nsa_f_general_allowance.list);" type="checkbox" name="list" id="<?php echo 'checkcus'.$i?>" value="<?=$row[id]?>"/></div></td>
									<td ><?php echo $row[number_time_work];?></td>
									<td><?php echo $row[number_max_holiday];?></td>
									<td ><?php echo number_format($row[allowance_holiday]);?></td>
									<td ><?php echo number_format($row[allowance_lunch]);?></td>
									<td ><?php echo number_format($row[allowance_car]);?></td>
									<td><?php echo number_format($row[allowance_hard]);?></td>
									<td ><?php echo number_format($row[allowance_overtime]);?></td>
									<td >
										<?php 
											if($row[is_status]==1){
												echo $l_active;
											}else{
												echo $l_status_saved;
											}
										?>
									</td>
									</tr>
								<?php 
									$i++;
								}
							}
						?>
					</tbody>
				</table>
			</div>
			<div style="margin: 10px 7px 5px 3px; color: #508E21; border-bottom: 1px dotted silver"> <?php echo $l_general_allowance;?> </div>
			<div id="showinput" style="width:100%">
				<table style="border-bottom: 1px dotted silver; padding:10px 5px" width="100%">
					<tr>
						<td class="tdfirst" ><?php echo $l_number_time_work;?>:<span style="color:red;font-weight: bold;float:right;">(*)</span></td>
						<td><input type="text" onkeypress="return enterTable(event,this.id);" onchange="checkname(this.value);" name="number_time_work" id="number_time_work"  class="in_testfield" style="width:516px" /></td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_number_max_holiday;?>:<span style="color:red;font-weight: bold;float:right;">(*)</span></td>
					<td><input type="text" onkeypress="return enterTable(event,this.id);" name="number_max_holiday" id="number_max_holiday" class="in_testfield" style="width:516px" /></td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_allowance_holiday;?>:<span style="color:red;font-weight: bold;float:right;">(*)</span></td>
					<td><input type="text" value="<?=number_format($_REQUEST['allowance_holiday'])?>" onkeypress="return enterTable(event,this.id);" id="allowance_holiday" name="allowance_holiday" class="in_testfield" style="width:516px" /> </td>
					</tr>
					<tr>
						<td class="tdfirst"><?php echo $l_allowance_lunch;?>:<span style="color:red;font-weight: bold;float:right;">(*)</span></td>
						<td><input type="text" value="<?=number_format($_REQUEST['allowance_lunch'])?>" onkeypress="return enterTable(event,this.id);" onchange="checkname(this.value);" name="allowance_lunch" id="allowance_lunch"  class="in_testfield" style="width:516px" /></td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_allowance_car;?>:<span style="color:red;font-weight: bold;float:right;">(*)</span></td>
					<td><input type="text" value="<?=number_format($_REQUEST['allowance_car'])?>" onkeypress="return enterTable(event,this.id);" name="allowance_car" id="allowance_car" class="in_testfield" style="width:516px" /></td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_allowance_hard;?>:<span style="color:red;font-weight: bold;float:right;">(*)</span></td>
					<td><input type="text" value="<?=number_format($_REQUEST['allowance_hard'])?>" onkeypress="return enterTable(event,this.id);" id="allowance_hard" name="allowance_hard" class="in_testfield" style="width:516px" /> </td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_allowance_overtime;?>:<span style="color:red;font-weight: bold;float:right;">(*)</span></td>
					<td><input type="text" value="<?=number_format($_REQUEST['allowance_overtime'])?>" onkeypress="return enterTable(event,this.id);" id="allowance_overtime" name="allowance_overtime" class="in_testfield" style="width:516px" /> </td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_Activestatus;?>:</td>
					<td>
						<input type="radio"  id="abc" name="abc" value="1" <?php if ($_REQUEST["abc"]=="1") echo "checked";?> /> <?php echo $l_apdung;?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input type="radio" name="abc" id="abc"  value="0" <?php if ($_REQUEST["abc"]=="0") echo "checked";?>/> <?php echo $l_status_saved;?>
					</td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_user_create;?>:</td>
					<td><input type="text" value="<?php echo $_REQUEST['user_create'];?>" onkeypress="return enterTable(event,this.id);" id="user_create" name="user_create" class="in_testfield" style="width:516px;background: #d7d9dd;"  readonly="readonly" /> </td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_first_create;?>:</td>
					<td><input type="text" value="<?php echo $_REQUEST['first_create'];?>" onkeypress="return enterTable(event,this.id);" id="first_create" name="first_create" class="in_testfield" style="width:516px;background: #d7d9dd;"  readonly="readonly" /> </td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_user_last_update;?>:</td>
					<td><input type="text" onkeypress="return enterTable(event,this.id);" id="user_last_update" name="user_last_update" class="in_testfield" style="width:516px;background: #d7d9dd;" readonly="readonly" /> </td>
					</tr>
					<tr>
					<td class="tdfirst"><?php echo $l_time_last_update;?>:</td>
					<td><input type="text" onkeypress="return enterTable(event,this.id);" id="time_last_update" name="time_last_update" class="in_testfield" style="width:516px;background: #d7d9dd;" readonly="readonly" /> </td>
					</tr>
				</table>
			</div>
			
			<div>
				<input type="hidden" name="content" id="content" value="genera_allowance" />
				<input type="hidden" name="userCreate" id="userCreate" value="<?=$_SESSION['fullname']?>" />
				<input type="hidden" name="hanhdong" id="hanhdong" value="addnew" />
				<input type="hidden" name="idgen" id="idgen" value="null" />
			</div>
			<table style="border-bottom: 1px dotted silver; padding:10px 5px" width="100%">
				<tr>
					<td class="tdfirst" style="background: none;"></td>
					<td style="padding-left: 45px">
						<input onclick="thucthi_add('<?php echo $_SESSION['IDEmployee'];?>');" id="AddNew" name="AddNew" style="margin-right:3px" type="button" value="<?php echo $l_add;?>" class="btnupbg btnupload" />
						<input onclick="thucthi_update('<?php echo $_SESSION['IDEmployee'];?>');" id="Update" name="Update" style="margin-right:3px" type="button" value="<?php echo $l_update;?>" class="btnupbg btnupload" />
						<input onclick="thucthi_delete();" id="Delete" name="Delete" style="margin-right:3px" type="button" value="<?php echo $l_delete;?>" class="btnupbg btnupload" />
						<input onclick="resetvalue();" id="Reset" name="Reset" style="margin-right:3px" type="button" value="<?php echo $l_reset;?>" class="btnupbg btnupload" />
					</td>
				</tr>
			</table>
		</div>
	</div>
</div>
</form>